//
//  LSYFastLookHotTableViewCell.m
//  Cartoon
//
//  Created by dllo on 15/7/16.
//  Copyright (c) 2015年 dllo. All rights reserved.
//
#import "BaseHeader.h"
#import "LSYFastLookHotTableViewCell.h"
#import "UIImageView+WebCache.h"

@interface LSYFastLookHotTableViewCell ()
@property (nonatomic, retain) UIButton *buttonLike;
@property (nonatomic, assign) int j;
@end
@implementation LSYFastLookHotTableViewCell


- (void)dealloc
{
    
    [_likeCount release];
    [_model release];
    [_userHead release];
    [_comment release];
    [_time release];
    [_userName release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.userHead = [[UIImageView alloc] init];
        [self.contentView addSubview:self.userHead];
        [_userName release];
        
        self.userName = [[UILabel alloc] init];
        [self.contentView addSubview:self.userName];
        [_userName release];
        
        self.time = [[UILabel alloc] init];
        [self.contentView addSubview:self.time];
        [_time release];
        
        self.comment = [[UILabel alloc] init];
        [self.contentView addSubview:self.comment];
        [_comment release];
        
        self.buttonLike = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.buttonLike];
        
        self.likeCount = [[UILabel alloc] init];
        [self.contentView addSubview:self.likeCount];
        [_likeCount release];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.userHead.frame = CGRectMake(15, 10, 50 * self.contentView.frame.size.width / 375, 50 * self.contentView.frame.size.width / 375);
    self.userHead.layer.cornerRadius = 25;
    self.userHead.layer.borderWidth = 0.1;
    self.userHead.clipsToBounds = YES;
    
    self.userName.frame = CGRectMake(self.userHead.frame.size.width + 10 + 10, self.userHead.frame.origin.y, self.contentView.frame.size.width - self.userHead.frame.size.width - 10 - 70, 20 * SCREEN_HEIGHT / 667);
    self.userName.font = [UIFont systemFontOfSize:13];
    
    self.time.frame = CGRectMake(self.userName.frame.origin.x, self.userName.frame.origin.y + self.userName.frame.size.height + 10, self.userName.frame.size.width / 2, 18 * SCREEN_HEIGHT / 667);
    //评论内容
    self.comment.frame = CGRectMake(self.time.frame.origin.x, self.time.frame.origin.y + self.time.frame.size.height + 10, self.contentView.frame.size.width - self.userHead.frame.size.width - 10 - 15, [[self class]heightWithText:self.comment.text]);
    self.comment.numberOfLines = 0;
    self.comment.font = [UIFont systemFontOfSize:12];
    
    self.buttonLike.frame = CGRectMake(self.userName.frame.origin.x + self.userName.frame.size.width, self.userName.frame.origin.y, 20 * SCREEN_WIDTH / 375, 20 * SCREEN_HEIGHT / 667);
    [self.buttonLike setImage:[UIImage imageNamed:@"iconfont-zan2"] forState:UIControlStateNormal];
    [self.buttonLike setImage:[UIImage imageNamed:@"iconfont-zan1"] forState:UIControlStateSelected];
    self.buttonLike.alpha = 0.7;
    [self.buttonLike addTarget:self action:@selector(clickLike:) forControlEvents:UIControlEventTouchUpInside];
    
    self.likeCount.frame = CGRectMake(self.buttonLike.frame.origin.x + self.buttonLike.frame.size.width + 5, self.buttonLike.frame.origin.y + 2.5 * SCREEN_HEIGHT / 667, 50 * SCREEN_WIDTH / 375, 15 * SCREEN_HEIGHT / 667);
    self.likeCount.font = [UIFont systemFontOfSize:10];
}
- (void)clickLike:(UIButton *)button
{
    self.j++;
    if (self.j % 2 != 0) {
        button.selected = YES;
    } else if (self.j %2 == 0){
        button.selected = NO;
    }
    
}


+ (CGFloat)heightWithText:(NSString *)text

{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGSize size = CGSizeMake(SCREEN_WIDTH - 25 * SCREEN_WIDTH / 375, 1000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
    
}

- (void)setModel:(LSYFastLookHotCommentsModel *)model
{
    self.comment.text = model.content;
    self.userName.text = [model.user objectForKey:@"nickname"];
    NSString *userHeaderStr = [model.user objectForKey:@"avatar_url"];
    [self.userHead sd_setImageWithURL:[NSURL URLWithString:userHeaderStr] placeholderImage:[UIImage imageNamed:@"touxiang.jpg"]];
    self.likeCount.text = model.likes_count.stringValue;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
